<?php
require_once '../application/library/helper/messageHelper.php';
require_once '../application/library/helper/csvHelper.php';
require_once '../application/library/cache/gcSession.php';
require_once '../application/library/controller/gcLBasicAction.php';
require_once '../application/library/exception/gcExceptionDealer.php';
require_once '../application/library/helper/sessionHelper.php';
require_once '../application/modules/goods/constant/goods_constant_templatName.php';
require_once '../application/modules/goods/constant/goods_constant_constant.php';
require_once '../application/modules/common/services/common_services_categoryTagService.php';
require_once '../application/modules/goods/services/goods_services_goodsListService.php';


/**
 * 商品一览Controller
 * 商品管理
 * @author    Suda
 * @version   1.0
 */
class goods_goodsListController extends gcLBasicAction {

    private $objService;
    private $objCategoryTagService;
    /**
     * 初期表示
     */
    public function indexAction() {
        try {
            $this->initMenu("1");
            $this->boolUserTypeCheck("1");
            $this->pvSetLabel();
            $this->objService = new goods_services_goodsListService();
            if (empty($arrParameter)) {
                $arrParameter = array();
                $arrParameter["curPage"] = 1;
                $arrParameter["pageSize"] = 10;
            }
            if($this->getParam("insertFg") == 1) {
                $this->setErrorMsg($this->getMessage("ERR-100003"));
            }
            if($this->getParam("updateFg") == 1) {
                $this->setErrorMsg($this->getMessage("ERR-100004"));
            }
            if($this->getParam("DeleteFg") == 1) {
                $this->setErrorMsg($this->getMessage("ERR-100005"));
            }
            if($this->getParam("returnFg") == 1) {
                $arrCondition = sessionHelper::getPageData(goods_constant_constant::GOODS_LIST);
//                var_dump($arrCondition);exit;
                $intCount = $this->objService->pbGetSearchCount($arrCondition);
                if (empty($arrCondition)) {
                    $arrCondition = array();
                    $arrCondition["curPage"] = 1;
                    $arrCondition["pageSize"] = 10;
                }
                $objPager=$this->pagerInit($intCount, $arrCondition["curPage"], $arrCondition["pageSize"], pageSizeGroup::PAGE_SIZE_TEN, display::SHOW);
                $arrCondition["offset"]      = $objPager->getOffset();
                $arrCondition["limit"]        = $objPager->getLimit();
                $arrGoods = $this->objService->pbSearchGoods($arrCondition);
//                sessionHelper::removePageData(goods_constant_constant::GOODS_LIST);
            } else {
                $intCount = $this->objService->pbGetCount();
                $objPager=$this->pagerInit($intCount, $arrParameter["curPage"], $arrParameter["pageSize"], pageSizeGroup::PAGE_SIZE_TEN, display::SHOW);
                $arrGoods = $this->objService->pbGetGoods();
                sessionHelper::removePageData(goods_constant_constant::GOODS_LIST);
            }
            $arrMsgId = array("ERR-100002");
            $this->setMessageForJs($arrMsgId);
//            $objPager=$this->pagerInit($intCount, $arrParameter["curPage"], $arrParameter["pageSize"], pageSizeGroup::PAGE_SIZE_TEN, display::SHOW);
            $this->objCategoryTagService       = new common_services_categoryTagService();
            $this->gcView->arrGoodsFirstCATG   = $this->objCategoryTagService->pbGetGoodsFirstCategory();
//            $arrCondition["FirstCatg"] = "1";
            if ($arrCondition["FirstCatg"] != "" && $arrCondition["FirstCatg"] != null) {
                $this->gcView->arrGoodsSecondCATG = $this->objCategoryTagService->pbGetGoodsSecondCategory($arrCondition["FirstCatg"]);
            } else {}
            $this->gcView->selGoodsFirstCATG   = $arrCondition["FirstCatg"];
            $this->gcView->selGoodsSecondCATG  = $arrCondition["SecondCatg"];
            $this->gcView->intGoodsNum = $intCount;
            $this->gcView->arrGoods = $arrGoods;
            $this->gcView->arrCondition = $arrCondition;
            $this->gcView->strNumFlg    = $arrCondition["StockNumFg"];
//            var_dump($count);
//            var_dump($arrGoods);exit;
            $this->gcView->render(goods_constant_templatName::GOODS_LIST);
        } catch(Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }

    /**
     * 初期化页面情报
     * pvSetLabel
     * @param null
     * @return null
     */
    private function pvSetLabel() {
        $this->gcView->title                          = $this->getLabel("goods_EC0001_PageTitle");
        $this->gcView->goods_EC0001_GoodsCode         = $this->getLabel("goods_EC0001_GoodsCode");
        $this->gcView->goods_EC0001_GoodsName         = $this->getLabel("goods_EC0001_GoodsName");
        $this->gcView->goods_EC0001_SalesPrice        = $this->getLabel("goods_EC0001_SalesPrice");
        $this->gcView->goods_EC0001_Yen               = $this->getLabel("goods_EC0001_Yen");
        $this->gcView->goods_EC0001_addGoods          = $this->getLabel("goods_EC0001_addGoods");
        $this->gcView->goods_EC0001_Link              = $this->getLabel("goods_EC0001_Link");
        $this->gcView->goods_EC0001_Class             = $this->getLabel("goods_EC0001_Class");
        $this->gcView->goods_EC0001_InNum             = $this->getLabel("goods_EC0001_InNum");
        $this->gcView->goods_EC0001_GetAll            = $this->getLabel("goods_EC0001_GetAll");
        $this->gcView->goods_EC0001_GetLeft           = $this->getLabel("goods_EC0001_GetLeft");
        $this->gcView->goods_EC0001_GetExcut          = $this->getLabel("goods_EC0001_GetExcut");
        $this->gcView->goods_EC0001_GetSet            = $this->getLabel("goods_EC0001_GetSet");
        $this->gcView->goods_EC0001_SearCondition     = $this->getLabel("goods_EC0001_SearCondition");
        $this->gcView->goods_EC0001_Clear             = $this->getLabel("goods_EC0001_Clear");
        $this->gcView->goods_EC0001_No                = $this->getLabel("goods_EC0001_No");
        $this->gcView->goods_EC0001_Image             = $this->getLabel("goods_EC0001_Image");
        $this->gcView->goods_EC0001_SalePrice         = $this->getLabel("goods_EC0001_SalePrice");
        $this->gcView->goods_EC0001_H1                = $this->getLabel("goods_EC0001_H1");
        $this->gcView->goods_EC0001_H2                = $this->getLabel("goods_EC0001_H2");
        $this->gcView->goods_EC0001_Title2            = $this->getLabel("goods_EC0001_Title2");
        $this->gcView->goods_EC0001_Label             = $this->getLabel("goods_EC0001_Label");
        $this->gcView->goods_EC0001_Num               = $this->getLabel("goods_EC0001_Num");
        $this->gcView->goods_EC0001_Variation         = $this->getLabel("goods_EC0001_Variation");
        $this->gcView->goods_EC0001_SupplierNum       = $this->getLabel("goods_EC0001_SupplierNum");
        $this->gcView->goods_EC0001_SupplierName      = $this->getLabel("goods_EC0001_SupplierName");
    }

    /**
     * 检索
     */
    public function searchAction() {
        try {
            if ($this->getRequest()->isXmlHttpRequest()) {
                $strFlag = $this->getParam("Flag");
                if ($strFlag == "1") {
                    $arrCondition = array("GoodsCode"  => $this->getParam("GoodsCode"),
                                      "GoodsName"  => $this->getParam("GoodsName"),
                                      "PriceFrom"  => $this->getParam("PriceFrom"),
                                      "PriceTo"    => $this->getParam("PriceTo"),
                                      "FirstCatg"    => $this->getParam("FirstCatg"),
                                      "curPage"    => $this->getParam("curPage"),
                                      "pageSize"    => $this->getParam("pageSize"),
                                      "SecondCatg"    => $this->getParam("SecondCatg"),
                                      "StockNumFg"    => $this->getParam("StockNumFg"),
                                      "StockNumFrom"    => $this->getParam("StockNumFrom"),
                                      "StockNumTo"    => $this->getParam("StockNumTo"));
//                    sessionHelper::setPageData(goods_constant_constant::GOODS_LIST, $arrCondition);
                } else {
                    $arrCondition = sessionHelper::getPageData(goods_constant_constant::GOODS_LIST);
                    $arrCondition["curPage"] = $this->getParam("curPage");
                    $arrCondition["pageSize"] = $this->getParam("pageSize");
                    $arrCondition["orderColumn"] = $this->getParam("orderColumn");
                    $arrCondition["order"] = $this->getParam("order");
                }
                $this->objService   = new goods_services_goodsListService();
                $intCount = $this->objService->pbGetSearchCount($arrCondition);
//                var_dump($objPager);exit;
                $objPager = $this->getPagerParam($intCount);
                $arrCondition["curPage"] = $objPager->getCurPage();
//                var_dump($arrCondition);exit;
                sessionHelper::setPageData(goods_constant_constant::GOODS_LIST, $arrCondition);
                if ($intCount != 0) {
                    $arrCondition["offset"]      = $objPager->getOffset();
                    $arrCondition["limit"]        = $objPager->getLimit();
                    $CurPage                         = $objPager->getCurPage();

                    $arrData = $this->objService->pbSearchGoods($arrCondition);
//                    var_dump($arrCondition);
//                    var_dump($intCount);var_dump($arrData);exit;
                    $arrResault = array(
                        "count"      => $intCount,
                        "curPage"      => $CurPage,
                        "searchData" => $arrData
                    );
//                    var_dump($arrResault);exit;
                } else {
                    $arrResault = array(
                        "count"      => 0,
                        "curPage"    => $objPager->getCurPage(),
                        "searchData" => "");
//                    var_dump($arrData);exit;
                }
                jsonHelper::outPutJson($arrResault);
            } else {
                gcExceptionDealer::throwGcException();
            }
        } catch(Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }

}